package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.CureTaskMedicineDAO;
import cn.edu.scau.cmi.hombio.dao.CureTaskMethodDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineDAO;
import cn.edu.scau.cmi.hombio.dao.UnitDAO;

import cn.edu.scau.cmi.hombio.domain.CureTaskMedicine;
import cn.edu.scau.cmi.hombio.domain.CureTaskMethod;
import cn.edu.scau.cmi.hombio.domain.Medicine;
import cn.edu.scau.cmi.hombio.domain.Unit;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for CureTaskMedicine entities
 * 
 */

@Service("CureTaskMedicineService")

@Transactional
public class CureTaskMedicineServiceImpl implements CureTaskMedicineService {

	/**
	 * DAO injected by Spring that manages CureTaskMedicine entities
	 * 
	 */
	@Autowired
	private CureTaskMedicineDAO cureTaskMedicineDAO;

	/**
	 * DAO injected by Spring that manages CureTaskMethod entities
	 * 
	 */
	@Autowired
	private CureTaskMethodDAO cureTaskMethodDAO;

	/**
	 * DAO injected by Spring that manages Medicine entities
	 * 
	 */
	@Autowired
	private MedicineDAO medicineDAO;

	/**
	 * DAO injected by Spring that manages Unit entities
	 * 
	 */
	@Autowired
	private UnitDAO unitDAO;

	/**
	 * Instantiates a new CureTaskMedicineServiceImpl.
	 *
	 */
	public CureTaskMedicineServiceImpl() {
	}

	/**
	 * Delete an existing Medicine entity
	 * 
	 */
	@Transactional
	public CureTaskMedicine deleteCureTaskMedicineMedicine(Integer curetaskmedicine_id, Integer related_medicine_id) {
		CureTaskMedicine curetaskmedicine = cureTaskMedicineDAO.findCureTaskMedicineByPrimaryKey(curetaskmedicine_id, -1, -1);
		Medicine related_medicine = medicineDAO.findMedicineByPrimaryKey(related_medicine_id, -1, -1);

		curetaskmedicine.setMedicine(null);
		related_medicine.getCureTaskMedicines().remove(curetaskmedicine);
		curetaskmedicine = cureTaskMedicineDAO.store(curetaskmedicine);
		cureTaskMedicineDAO.flush();

		related_medicine = medicineDAO.store(related_medicine);
		medicineDAO.flush();

		medicineDAO.remove(related_medicine);
		medicineDAO.flush();

		return curetaskmedicine;
	}

	/**
	 * Save an existing Unit entity
	 * 
	 */
	@Transactional
	public CureTaskMedicine saveCureTaskMedicineUnit(Integer id, Unit related_unit) {
		CureTaskMedicine curetaskmedicine = cureTaskMedicineDAO.findCureTaskMedicineByPrimaryKey(id, -1, -1);
		Unit existingunit = unitDAO.findUnitByPrimaryKey(related_unit.getId());

		// copy into the existing record to preserve existing relationships
		if (existingunit != null) {
			existingunit.setId(related_unit.getId());
			existingunit.setName(related_unit.getName());
			existingunit.setMemo(related_unit.getMemo());
			related_unit = existingunit;
		}

		curetaskmedicine.setUnit(related_unit);
		related_unit.getCureTaskMedicines().add(curetaskmedicine);
		curetaskmedicine = cureTaskMedicineDAO.store(curetaskmedicine);
		cureTaskMedicineDAO.flush();

		related_unit = unitDAO.store(related_unit);
		unitDAO.flush();

		return curetaskmedicine;
	}

	/**
	 * Save an existing CureTaskMedicine entity
	 * 
	 */
	@Transactional
	public void saveCureTaskMedicine(CureTaskMedicine curetaskmedicine) {
		CureTaskMedicine existingCureTaskMedicine = cureTaskMedicineDAO.findCureTaskMedicineByPrimaryKey(curetaskmedicine.getId());

		if (existingCureTaskMedicine != null) {
			if (existingCureTaskMedicine != curetaskmedicine) {
				existingCureTaskMedicine.setId(curetaskmedicine.getId());
				existingCureTaskMedicine.setQuantity(curetaskmedicine.getQuantity());
				existingCureTaskMedicine.setMemo(curetaskmedicine.getMemo());
			}
			curetaskmedicine = cureTaskMedicineDAO.store(existingCureTaskMedicine);
		} else {
			curetaskmedicine = cureTaskMedicineDAO.store(curetaskmedicine);
		}
		cureTaskMedicineDAO.flush();
	}

	/**
	 * Return a count of all CureTaskMedicine entity
	 * 
	 */
	@Transactional
	public Integer countCureTaskMedicines() {
		return ((Long) cureTaskMedicineDAO.createQuerySingleResult("select count(o) from CureTaskMedicine o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing CureTaskMethod entity
	 * 
	 */
	@Transactional
	public CureTaskMedicine deleteCureTaskMedicineCureTaskMethod(Integer curetaskmedicine_id, Integer related_curetaskmethod_id) {
		CureTaskMedicine curetaskmedicine = cureTaskMedicineDAO.findCureTaskMedicineByPrimaryKey(curetaskmedicine_id, -1, -1);
		CureTaskMethod related_curetaskmethod = cureTaskMethodDAO.findCureTaskMethodByPrimaryKey(related_curetaskmethod_id, -1, -1);

		curetaskmedicine.setCureTaskMethod(null);
		related_curetaskmethod.getCureTaskMedicines().remove(curetaskmedicine);
		curetaskmedicine = cureTaskMedicineDAO.store(curetaskmedicine);
		cureTaskMedicineDAO.flush();

		related_curetaskmethod = cureTaskMethodDAO.store(related_curetaskmethod);
		cureTaskMethodDAO.flush();

		cureTaskMethodDAO.remove(related_curetaskmethod);
		cureTaskMethodDAO.flush();

		return curetaskmedicine;
	}

	/**
	 * Delete an existing Unit entity
	 * 
	 */
	@Transactional
	public CureTaskMedicine deleteCureTaskMedicineUnit(Integer curetaskmedicine_id, Integer related_unit_id) {
		CureTaskMedicine curetaskmedicine = cureTaskMedicineDAO.findCureTaskMedicineByPrimaryKey(curetaskmedicine_id, -1, -1);
		Unit related_unit = unitDAO.findUnitByPrimaryKey(related_unit_id, -1, -1);

		curetaskmedicine.setUnit(null);
		related_unit.getCureTaskMedicines().remove(curetaskmedicine);
		curetaskmedicine = cureTaskMedicineDAO.store(curetaskmedicine);
		cureTaskMedicineDAO.flush();

		related_unit = unitDAO.store(related_unit);
		unitDAO.flush();

		unitDAO.remove(related_unit);
		unitDAO.flush();

		return curetaskmedicine;
	}

	/**
	 * Delete an existing CureTaskMedicine entity
	 * 
	 */
	@Transactional
	public void deleteCureTaskMedicine(CureTaskMedicine curetaskmedicine) {
		cureTaskMedicineDAO.remove(curetaskmedicine);
		cureTaskMedicineDAO.flush();
	}

	/**
	 */
	@Transactional
	public CureTaskMedicine findCureTaskMedicineByPrimaryKey(Integer id) {
		return cureTaskMedicineDAO.findCureTaskMedicineByPrimaryKey(id);
	}

	/**
	 * Return all CureTaskMedicine entity
	 * 
	 */
	@Transactional
	public List<CureTaskMedicine> findAllCureTaskMedicines(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<CureTaskMedicine>(cureTaskMedicineDAO.findAllCureTaskMedicines(startResult, maxRows));
	}

	/**
	 * Save an existing Medicine entity
	 * 
	 */
	@Transactional
	public CureTaskMedicine saveCureTaskMedicineMedicine(Integer id, Medicine related_medicine) {
		CureTaskMedicine curetaskmedicine = cureTaskMedicineDAO.findCureTaskMedicineByPrimaryKey(id, -1, -1);
		Medicine existingmedicine = medicineDAO.findMedicineByPrimaryKey(related_medicine.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicine != null) {
			existingmedicine.setId(related_medicine.getId());
			existingmedicine.setName(related_medicine.getName());
			existingmedicine.setNumber(related_medicine.getNumber());
			existingmedicine.setCompany(related_medicine.getCompany());
			existingmedicine.setQuantityPerCase(related_medicine.getQuantityPerCase());
			existingmedicine.setQuantityPerBox(related_medicine.getQuantityPerBox());
			existingmedicine.setPhone(related_medicine.getPhone());
			existingmedicine.setMemo(related_medicine.getMemo());
			related_medicine = existingmedicine;
		}

		curetaskmedicine.setMedicine(related_medicine);
		related_medicine.getCureTaskMedicines().add(curetaskmedicine);
		curetaskmedicine = cureTaskMedicineDAO.store(curetaskmedicine);
		cureTaskMedicineDAO.flush();

		related_medicine = medicineDAO.store(related_medicine);
		medicineDAO.flush();

		return curetaskmedicine;
	}

	/**
	 * Save an existing CureTaskMethod entity
	 * 
	 */
	@Transactional
	public CureTaskMedicine saveCureTaskMedicineCureTaskMethod(Integer id, CureTaskMethod related_curetaskmethod) {
		CureTaskMedicine curetaskmedicine = cureTaskMedicineDAO.findCureTaskMedicineByPrimaryKey(id, -1, -1);
		CureTaskMethod existingcureTaskMethod = cureTaskMethodDAO.findCureTaskMethodByPrimaryKey(related_curetaskmethod.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcureTaskMethod != null) {
			existingcureTaskMethod.setId(related_curetaskmethod.getId());
			existingcureTaskMethod.setMemo(related_curetaskmethod.getMemo());
			related_curetaskmethod = existingcureTaskMethod;
		}

		curetaskmedicine.setCureTaskMethod(related_curetaskmethod);
		related_curetaskmethod.getCureTaskMedicines().add(curetaskmedicine);
		curetaskmedicine = cureTaskMedicineDAO.store(curetaskmedicine);
		cureTaskMedicineDAO.flush();

		related_curetaskmethod = cureTaskMethodDAO.store(related_curetaskmethod);
		cureTaskMethodDAO.flush();

		return curetaskmedicine;
	}

	/**
	 * Load an existing CureTaskMedicine entity
	 * 
	 */
	@Transactional
	public Set<CureTaskMedicine> loadCureTaskMedicines() {
		return cureTaskMedicineDAO.findAllCureTaskMedicines();
	}
}
